package com.fengchunge.service.impl;

import com.fengchunge.common.CollectionStatusEnum;
import com.fengchunge.common.ServerResponse;
import com.fengchunge.dao.UserCollectionBackMapper;
import com.fengchunge.pojo.UserCollectionBack;
import com.fengchunge.service.IUserOperationService;
import com.fengchunge.util.LongIdGenerator;
import com.fengchunge.vo.CollectionResponseVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: mingtai
 * @createDate: 23:37 2021/12/5
 * @version: 1.0
 */
@Slf4j
@Service("IUserOperationService")
public class UserOperationServiceImpl implements IUserOperationService {

    @Autowired
    private UserCollectionBackMapper userCollectionBackMapper;
    private LongIdGenerator generator = new LongIdGenerator(1L);

    @Override
    public ServerResponse<CollectionResponseVo> collectionMovie(UserCollectionBack userCollectionBack) {
        checkParam(userCollectionBack);
        UserCollectionBack userCollectionBacks = userCollectionBackMapper.selectByUidAndMovieUrl(userCollectionBack.getUid(), userCollectionBack.getMovieUrl());
        if (userCollectionBacks !=null && userCollectionBacks.getCollectionStatus().equals(CollectionStatusEnum.COLLECTION_YES.getCode())) {
            return ServerResponse.createByErrorMessage("已收藏该影片,不能重复收藏");
        }
        // 已取消 有这条数据，就不能新增了，更新
        if (userCollectionBacks != null && userCollectionBacks.getCollectionStatus().equals(CollectionStatusEnum.COLLECTION_NO.getCode())) {
            userCollectionBack.setCollectionStatus(CollectionStatusEnum.COLLECTION_YES.getCode());
            userCollectionBack.setMovieUrl(userCollectionBacks.getMovieUrl());
            int i = userCollectionBackMapper.updateByMovieUrlSelective(userCollectionBack);
            CollectionResponseVo collectionResponseVo = CollectionResponseVo.builder().movieId(userCollectionBack.getMovieId()).uid(userCollectionBack.getUid()).build();
            return i == 1 ? ServerResponse.createBySuccess(collectionResponseVo) : ServerResponse.createByErrorMessage("收藏失败，请联系管理员");
        }
        userCollectionBack.setCreateTime(new Date());
        // 无法自动更新,需要手动设置时间
        userCollectionBack.setUpdateTime(new Date());
        Long movieId = generator.generate();
        userCollectionBack.setMovieId(movieId);
        userCollectionBack.setCollectionStatus(CollectionStatusEnum.COLLECTION_YES.getCode());
        int insertSelective = userCollectionBackMapper.insertSelective(userCollectionBack);
        if (insertSelective == 1) {
            CollectionResponseVo collectionResponseVo = CollectionResponseVo.builder().movieId(movieId).uid(userCollectionBack.getUid()).build();
            return ServerResponse.createBySuccess(collectionResponseVo);
        }
        return ServerResponse.createByErrorMessage("收藏失败");
    }

    private void checkParam(UserCollectionBack userCollectionBack) {
        Validate.isTrue(StringUtils.isNoneEmpty(userCollectionBack.getMovieUrl()), "影片地址不能为空");
        Validate.isTrue(userCollectionBack.getUid() != null, "uid不能为空");
        Validate.isTrue(StringUtils.isNoneEmpty(userCollectionBack.getMovieName()), "影片不能为空");
    }

    @Override
    public ServerResponse<Boolean> cancelCollectionMovie(Long uid, Long movieId) {
        UserCollectionBack userCollectionBack = new UserCollectionBack();
        List<UserCollectionBack> userCollectionBacks = userCollectionBackMapper.selectByUidAndMovieId(uid, movieId);
        log.info("查询的收藏列表 = {}", userCollectionBack);
        if (CollectionUtils.isEmpty(userCollectionBacks)) {
            return ServerResponse.createByErrorMessage("取消收藏失败，没有此条收藏记录");
        }
        for (UserCollectionBack collectionBack : userCollectionBacks) {
            if (!CollectionStatusEnum.COLLECTION_YES.getCode().equals(collectionBack.getCollectionStatus())) {
                return ServerResponse.createByErrorMessage("取消收藏失败，不是在收藏状态");
            }
        }
        userCollectionBack.setCollectionStatus(CollectionStatusEnum.COLLECTION_NO.getCode());
        userCollectionBack.setUpdateTime(new Date());
        userCollectionBack.setMovieId(movieId);
        userCollectionBack.setUid(uid);
        int i = userCollectionBackMapper.updateByPrimaryKeySelective(userCollectionBack);
        log.info("更新的收藏列表个数= {}", i);
        if (i > 0) {
            return ServerResponse.createBySuccess("取消成功", true);
        }
        return ServerResponse.createByErrorMessage("取消失败");
    }

    @Override
    public ServerResponse<PageInfo> getCollectionList(int pageNum,
                                                      int pageSize,
                                                      Long uid) {
        //startPage--start
        PageHelper.startPage(pageNum, pageSize);
        // 收藏列表
        List<UserCollectionBack> collectionList = userCollectionBackMapper.getCollectionList(uid, CollectionStatusEnum.COLLECTION_YES.getCode());
        PageInfo pageResult = new PageInfo(collectionList);
        return ServerResponse.createBySuccess(pageResult);
    }
}



